<template>
  <div class="flex flex-col h-full">
    <div class="flex-1 overflow-auto">
      <a-form ref="formRef" :model="formState" :label-col="{ style: { width: '100px' } }" class="mt-4 form-style-flex">
        <a-form-item label="主要诊断" class="w-full">
          <SelectTable
            v-model:value="formState.khoIcd10Name"
            type="诊断"
            class-name="w-full"
            placeholder="请选择"
            @clear="formState.khoIcd10 = ''"
            @select="admissionAiagnosis"
          />
        </a-form-item>
        <a-form-item name="appDeptCode" label="科室" class="w-1/4">
          <a-select v-model:value="formState.appDeptCode" placeholder="请选择" :options="departmentList" :field-names="{ label: 'ksmc', value: 'ksdm' }" />
        </a-form-item>
        <a-form-item name="appWardId" label="病区" class="w-1/4">
          <a-select
            v-model:value="formState.appWardId"
            :options="wardList"
            :field-names="{ label: 'wardName', value: 'id' }"
            placeholder="请选择"
            @change="handlerSelectArea"
          />
        </a-form-item>
        <a-form-item name="khoBedCode" label="床位号" class="w-1/2">
          <SelectTable
            v-model:value="formState.khoBedCode"
            type="床位"
            :placeholder="formState.appWardId ? '请选择' : '请选择病区'"
            :disabled="!formState.appWardId"
            :search-params="{ bqid: formState.appWardId }"
            :row-disabled="(row) => row.status === 2 || row.status === 1"
            @select="onSelectWard"
          />
        </a-form-item>

        <a-form-item name="memo" label="备注" class="w-full">
          <a-textarea v-model:value="formState.memo" :rows="15" placeholder="请输入" />
        </a-form-item>
      </a-form>
      <a-space class="flex justify-end h-10">
        <a-button @click="onPrint">打印</a-button>
        <a-button @click="getData">重置</a-button>
        <a-button type="primary" @click="saveData">保存</a-button>
      </a-space>
    </div>
  </div>
</template>
<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import { post, get } from '@/utils/request'
// import SelectTable from '@/components/SelectTable'
// import SelectArea from '@/components/SelectArea'
import SelectTable from '@/components/SelectTable'
import userStore from '@/store/user'
import { getSystemConfig } from '@/utils/systemConfig'

const { empId } = userStore()
import useStore from '../../store'

const isArmy = getSystemConfig('NAVYHIS_MODE') === '1' //军用系统
const store = useStore()
const formRef = ref(null)
const printData = ref({})
const printMe = ref()
const formState = ref({
  appDeptCode: null,
  appWardId: null,
  khoIcd10Name: null,
  khoBedCode: null,
  memo: null
})
const wardList = ref([])
// 员工信息（拿登录人科室）
const getEmpInfo = async () => {
  const { code, data, msg } = await get('/common/emp/getEmpInfo?empid=' + empId)
  if (code !== 0) return message.error(msg)
  formState.value.appDeptCode = data.ksdm
}

const handlerSelectArea = () => {
  formState.value.khoBedCode = null
}

//获取数据
const getData = async () => {
  const { data, code, msg } = await post('/outp/mzys/getOutpKhoRecordByJzbh', {
    jzbh: store.currentPatient.jzbh //就诊编号
  })
  if (code !== 0) return message.error(msg)
  if (data) {
    formState.value = data
    formState.value.bw = data.bw ? data.bw : undefined
    if (!data?.appDeptCode) {
      getEmpInfo()
    }
  } else {
    formRef.value.resetFields()
    formState.value.khoIcd10 = ''
    formState.value.appWardId = null
    getEmpInfo()
  }

  formState.value.khoIcd10Name = data?.khoIcd10Name || store.currentMedical?.bcLczdZdmc
  formState.value.khoIcd10 = data?.khoIcd10 || store.currentMedical?.bcLczdZddm
}

const saveData = async () => {
  const { code, msg } = await post('/outp/mzys/doSaveOutpKhoRecord', {
    ...formState.value,
    outpLsh: store.currentPatient.jzbh
  })
  if (code !== 0) return message.error(msg || '留观申请失败')
  message.success(msg)
  getData()
}

const onSelectWard = (select) => {
  formState.value.khoBedCode = select.bedCode
}

const admissionAiagnosis = (select) => {
  formState.value.khoIcd10Name = select.zdmc
  formState.value.khoIcd10 = select.zddm
}

// 获取预约病区
const getWardData = async () => {
  const { code, data } = await post('/operation/opex/getWardInfoList', { wardTypeCodes: ['QYLB001'] })
  if (code !== 0) return message.error(code)
  wardList.value = data
}

// 获取科室
const departmentList = ref([])
const getDepartmentList = async () => {
  const { code, data } = await post('/outp/mzdj/getDeptInfoList', { sfylks: '0' })
  if (code !== 0) return
  departmentList.value = data
}

// 打印
const onPrint = async () => {
  const { code, msg, data } = await post('/outp/outPrint/printOutpKhoRecord', { jzbh: store.currentPatient.jzbh })
  if (code !== 0) return message.error(msg)
  if (isArmy) {
    printData.value = JSON.parse(data)
    setTimeout(() => {
      printMe.value.$el.click()
    }, 1000)
  } else {
    window.openExe?.rpt(data) //调用winform打印
  }
}

onMounted(() => {
  getData()
  getWardData()
  getDepartmentList()
})
</script>
<style lang="less" scoped>
.ant-col {
  padding-right: 0 !important;
}
.ant-row {
  margin-right: 0 !important;
}
</style>
